package ezonsport.android.ui;

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.widget.DatePicker;
import ezonsport.android.R;
import ezonsport.android.application.PreferenceKeys;

public class UserInformationSettings extends PreferenceActivity implements OnPreferenceChangeListener{
	private static final String TAG="UserInformationSettings";
	static final int DATE_DIALOG_ID = 0; 
	private static final String USER_HEIGHT = "user_height";
	private static final String USER_WEIGHT = "user_weight";
	private static final String USER_GENDER = "user_gender";
	private static final String USER_BIRTHDAY = "user_birthday";
	private EditTextPreference m_heightPreference;
	private EditTextPreference m_weightPreference;
	private ListPreference m_genderPreference;
	private Preference mBirthdayPreference;
	//SharedPreferences
	private SharedPreferences mSharePre;
	private Editor editor;
	
	//Date
	private int mYear;
	private int mMonth;
	private int mDay;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.user_information_settings);
        m_heightPreference = (EditTextPreference)findPreference("user_height");
        m_weightPreference = (EditTextPreference)findPreference("user_weight");
        m_genderPreference = (ListPreference)findPreference("user_gender");
        mBirthdayPreference = (Preference)findPreference("user_birthday");
        m_heightPreference.setOnPreferenceChangeListener(this);
        m_weightPreference.setOnPreferenceChangeListener(this);
        m_genderPreference.setOnPreferenceChangeListener(this);
        mBirthdayPreference.setOnPreferenceChangeListener(this);
        mBirthdayPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener(){
			
			@Override
			public boolean onPreferenceClick(Preference preference) {
				// TODO Auto-generated method stub
				showDialog(DATE_DIALOG_ID);
				return false;
			}
		});
        mSharePre = getSharedPreferences(PreferenceKeys.SHAREPREFERENCENAME, 
        		MODE_PRIVATE);
        editor = mSharePre.edit();
        mYear = mSharePre.getInt(PreferenceKeys.USER_BIRTHDAY_YEAR, 0);
        mMonth = mSharePre.getInt(PreferenceKeys.USER_BIRTHDAY_MONTH, -1);
        mDay = mSharePre.getInt(PreferenceKeys.USER_BIRTHDAY_DAY, 0);
        initPreferences();
        if(mYear==0||mMonth==-1||mDay==0){
        	Log.v(TAG, "changetonow");
        	Calendar calendar = Calendar.getInstance();
        	mYear = calendar.get(Calendar.YEAR);
        	mMonth = calendar.get(Calendar.MONTH);
        	mDay = calendar.get(Calendar.DAY_OF_MONTH);
        }
    }
    @Override
    protected void onResume(){
    	super.onResume();
    	//initPreferences();
    }
    
    @Override
	public boolean onPreferenceChange(Preference preference, Object newValue){
    	String key = preference.getKey();
    	if(key.equals(USER_HEIGHT)){//未作校验
    		m_heightPreference.setSummary((String)newValue);
    		editor.putString(PreferenceKeys.USER_HEIGHT, (String)newValue);
    	}
    	if(key.equals(USER_WEIGHT)){
    		m_weightPreference.setSummary((String)newValue);
    		editor.putString(PreferenceKeys.USER_WEIGHT, (String)newValue);
    	}
    	if(key.equals(USER_GENDER)){
    		m_genderPreference.setSummary((String)newValue);
    		editor.putString(PreferenceKeys.USER_GENDER, (String)newValue);
    	}
    	if(key.equals(USER_BIRTHDAY)){
    		//dosomething
    	}
    	editor.commit();
    	return false;
    }
    @Override
    protected Dialog onCreateDialog(int id) {
    	switch(id){
    	case DATE_DIALOG_ID:
    		return new DatePickerDialog(this,
                    mDateSetListener,
                    mYear, mMonth, mDay);
    	}
    	return null;
    }
    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
    	
        switch (id) {
        case DATE_DIALOG_ID:
        	((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
            break;
        }
    }
    private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
                updateBirthdayDate();
            }
        };
    
    
    private void updateBirthdayDate() {
    	editor.putInt(PreferenceKeys.USER_BIRTHDAY_YEAR, mYear);
    	editor.putInt(PreferenceKeys.USER_BIRTHDAY_MONTH, mMonth);
    	editor.putInt(PreferenceKeys.USER_BIRTHDAY_DAY, mDay);
    	mBirthdayPreference.setSummary(
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mYear).append("-")
                    .append(mMonth + 1).append("-")
                    .append(mDay));
    	editor.commit();
    }
    
    private void initPreferences(){
    	Log.v(TAG, "initPreferences");
    	m_heightPreference.setSummary(mSharePre.getString(PreferenceKeys.USER_HEIGHT, ""));
    	m_weightPreference.setSummary(mSharePre.getString(PreferenceKeys.USER_WEIGHT, ""));
    	m_genderPreference.setSummary(mSharePre.getString(PreferenceKeys.USER_GENDER, ""));
    	if((mYear!=0)&&(mMonth!=-1)&&(mDay!=0)){
    		mBirthdayPreference.setSummary(String.valueOf(mYear)+"-"+String.valueOf(mMonth+1)+
    				"-" + String.valueOf(mDay));
    	}else
    		mBirthdayPreference.setSummary("");
    }
}
